package com.hellogood.yaohuo.client;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.basic.ColorTab;
import com.hellogood.yaohuo.client.basic.CupTab;
import com.hellogood.yaohuo.client.basic.SizeTab;
import com.hellogood.yaohuo.client.basic.ZiyouSetTab;
import com.hellogood.yaohuo.client.bussiness.fanchang.FanchangTab;
import com.hellogood.yaohuo.client.bussiness.pandian.PandianTab;
import com.hellogood.yaohuo.client.bussiness.peisong.PeisongTab;
import com.hellogood.yaohuo.client.bussiness.tuihuo.TuihuoTab;
import com.hellogood.yaohuo.client.bussiness.yanshou.YanshouTab;
import com.hellogood.yaohuo.client.bussiness.yaohuo.YaohuoTab;
import com.hellogood.yaohuo.client.bussiness.ziyou.ZiyouTab;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.client.common.LoginUser;
import com.hellogood.yaohuo.client.setting.GroupQxTab;
import com.hellogood.yaohuo.client.setting.GroupTab;
import com.hellogood.yaohuo.client.setting.QuyuTab;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Groupqx;
import com.hellogood.yaohuo.shared.services.GroupQxService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class MainPanel extends HLayout {
	private FunctionTreeNode[] functionData = null;
	private String loginUserCode = null;

	private TreeGrid treeGrid = null;
	private TabSet tabSet = null;

	private ColorTab colorTab = null;
	private CupTab cupTab = null;
	private SizeTab sizeTab = null;
	private ZiyouSetTab ziyouSetTab = null;

	private QuyuTab quyuTab = null;
	private GroupTab groupTab = null;
	private GroupQxTab groupQxTab = null;

	private YaohuoTab yaohuoTab = null;
	private YanshouTab yanshouTab = null;
	private PeisongTab peisongTab = null;
	private FanchangTab fanchangTab = null;
	private TuihuoTab tuihuoTab = null;
	private PandianTab pandianTab = null;
	private ZiyouTab ziyouTab = null;

	public MainPanel() {
		initData();
		initView();
	}

	private void initData() {
		loginUserCode = LoginUser.getInstance().getCurrentUser().getStaffCode();
		GroupQxService.Util.getInstance().getGroupQxListByUserCode(loginUserCode, new AsyncCallback<LinkedHashMap<String, Groupqx>>() {

			@Override
			public void onSuccess(LinkedHashMap<String, Groupqx> map) {
				List<FunctionTreeNode> list = new ArrayList<FunctionTreeNode>();
				if (loginUserCode.equals(Constants.ADMINISTRATOR_CODE)) {
					list.add(new FunctionTreeNode("1", "-1", "系统设置"));
					list.add(new FunctionTreeNode("5", "-1", "操作日志"));
					list.add(new FunctionTreeNode("2", "-1", "基本资料"));
					list.add(new FunctionTreeNode("3", "-1", "业务操作"));
					// list.add(new FunctionTreeNode("4", "-1", "报表查询"));
					list.add(new FunctionTreeNode("11", "1", "用户组设置"));
					list.add(new FunctionTreeNode("12", "1", "用户组权限"));
					list.add(new FunctionTreeNode("21", "2", "颜色设置"));
					list.add(new FunctionTreeNode("22", "2", "尺寸设置"));
					// list.add(new FunctionTreeNode("23", "2", "杯级设置"));
//					list.add(new FunctionTreeNode("24", "2", "自由项目设置"));
					list.add(new FunctionTreeNode("31", "3", "门店要货"));
					list.add(new FunctionTreeNode("34", "3", "总部汇总"));
					list.add(new FunctionTreeNode("37", "3", "总部汇总查询"));
					list.add(new FunctionTreeNode("38", "3", "配送"));
					list.add(new FunctionTreeNode("39", "3", "物流返厂"));
					list.add(new FunctionTreeNode("3a", "3", "物流验收"));
					list.add(new FunctionTreeNode("3b", "3", "配送退货"));
					// list.add(new FunctionTreeNode("41", "4", "库存查询"));
					// list.add(new FunctionTreeNode("42", "4", "台账查询"));
					list.add(new FunctionTreeNode("43", "3", "盘点"));
//					list.add(new FunctionTreeNode("44", "3", "自由补货"));
				} else {
					list.add(new FunctionTreeNode("3", "-1", "业务操作"));
					// list.add(new FunctionTreeNode("4", "-1", "报表查询"));
					if (map.containsKey(Groupqx.YAOHUO)) {
						list.add(new FunctionTreeNode("31", "3", "门店要货"));
					}
					if (map.containsKey(Groupqx.ZILIAO)) {
						list.add(new FunctionTreeNode("11", "1", "用户组设置"));
						list.add(new FunctionTreeNode("12", "1", "用户组权限"));
					}
					if (map.containsKey(Groupqx.ZONGBU_HUIZONG)) {
						list.add(new FunctionTreeNode("34", "3", "总部汇总"));
					}
					if (map.containsKey(Groupqx.ZB_HUIZONG_CHAXUN)) {
						list.add(new FunctionTreeNode("37", "3", "总部汇总查询"));
					}
					if (map.containsKey(Groupqx.PEISONG)) {
						list.add(new FunctionTreeNode("38", "3", "配送"));
					}
					if (map.containsKey(Groupqx.WULIU_FANCHANG)) {
						list.add(new FunctionTreeNode("39", "3", "物流返厂"));
					}
					if (map.containsKey(Groupqx.WULIU_YANSHOU)) {
						list.add(new FunctionTreeNode("3a", "3", "物流验收"));
					}
					if (map.containsKey(Groupqx.PEISONG_TUIHUO)) {
						list.add(new FunctionTreeNode("3b", "3", "配送退货"));
					}
					// if (map.containsKey(Groupqx.KUCUN_CHAXUN)) {
					// list.add(new FunctionTreeNode("41", "4", "库存查询"));
					// }
					// if (map.containsKey(Groupqx.TAIZHANG_CHAXUN)) {
					// list.add(new FunctionTreeNode("42", "4", "台账查询"));
					// }
					if (map.containsKey(Groupqx.PANDIAN)) {
						list.add(new FunctionTreeNode("43", "3", "盘点"));
					}
					if (map.containsKey(Groupqx.ZIYOU_BUHUO)) {
//						list.add(new FunctionTreeNode("44", "3", "自由补货"));
					}
				}
				functionData = new FunctionTreeNode[list.size()];
				for (int i = 0; i < functionData.length; i++) {
					functionData[i] = list.get(i);
				}
				if (functionData != null) {
					for (TreeNode node : functionData) {
						node.setAttribute("isOpen", false);
						if (node.getAttribute("parentId").equals("-1")) {
							node.setAttribute("isFolder", true);
						} else {
							node.setAttribute("isFolder", false);
						}
					}
				}
				Tree menuTree = new Tree();
				menuTree.setModelType(TreeModelType.PARENT);
				menuTree.setNameProperty("name");
				menuTree.setIsFolderProperty("isFolder");
				menuTree.setRootValue("-1");
				menuTree.setIdField("nodeId");
				menuTree.setParentIdField("parentId");
				menuTree.setOpenProperty("isOpen");
				menuTree.setData(functionData);
				treeGrid.setData(menuTree);
			}

			@Override
			public void onFailure(Throwable caught) {
				SC.say(caught.getMessage());
			}
		});
	}

	private void initView() {
		treeGrid = new TreeGrid();
		treeGrid.setFields(new TreeGridField("name", "操作"));
		treeGrid.setHeight("100%");
		treeGrid.addDoubleClickHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				Record r = treeGrid.getSelectedRecord();
				if (r != null) {
					String nodeID = r.getAttribute("nodeId");
					clickTreeNode(nodeID);
				}
			}
		});

		VLayout leftSideLayout = new VLayout();
		leftSideLayout.addMember(treeGrid);
		leftSideLayout.setWidth("200");
		leftSideLayout.setHeight("100%");
		addMember(leftSideLayout);

		tabSet = new TabSet();
		tabSet.setSize("100%", "100%");
		tabSet.addCloseClickHandler(new CloseClickHandler() {

			@Override
			public void onCloseClick(TabCloseClickEvent event) {
				event.cancel(); // MUST DO THIS FIRST
				SC.confirm(CommonMessages.ConfirmClose, new ConfirmBooleanCallback(event));
			}
		});
		VLayout rightSideLayout = new VLayout();
		rightSideLayout.setWidth("85%");
		rightSideLayout.setHeight("100%");
		rightSideLayout.addMember(tabSet);
		addMember(rightSideLayout);

		setMargin(10);
		setWidth100();
		setHeight100();
	}

	private class FunctionTreeNode extends TreeNode {
		public FunctionTreeNode(String nodeID, String parentID, String name) {
			setAttribute("nodeId", nodeID);
			setAttribute("parentId", parentID);
			setAttribute("name", name);
		}
	}

	private void clickTreeNode(String nodeID) {
		if (nodeID.equals(Constants.TREE_ID_OF_COLOR_SETTING)) {
			if (colorTab != null && tabSet.getTab(colorTab.getID()) != null) {
				tabSet.selectTab(colorTab);
			} else {
				colorTab = new ColorTab();
				colorTab.setTitle("颜色设置");
				tabSet.addTab(colorTab);
				tabSet.selectTab(colorTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_CUP_SETTING)) {
			if (cupTab != null && tabSet.getTab(cupTab.getID()) != null) {
				tabSet.selectTab(cupTab);
			} else {
				cupTab = new CupTab();
				cupTab.setTitle("杯级设置");
				tabSet.addTab(cupTab);
				tabSet.selectTab(cupTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_SIZE_SETTING)) {
			if (sizeTab != null && tabSet.getTab(sizeTab.getID()) != null) {
				tabSet.selectTab(sizeTab);
			} else {
				sizeTab = new SizeTab();
				sizeTab.setTitle("尺寸设置");
				tabSet.addTab(sizeTab);
				tabSet.selectTab(sizeTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_ZIYOU_SETTING)) {
			if (ziyouSetTab != null && tabSet.getTab(ziyouSetTab.getID()) != null) {
				tabSet.selectTab(ziyouSetTab);
			} else {
				ziyouSetTab = new ZiyouSetTab();
				ziyouSetTab.setTitle("自由项目设置");
				tabSet.addTab(ziyouSetTab);
				tabSet.selectTab(ziyouSetTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_QUYU_SETTING)) {
			if (quyuTab != null && tabSet.getTab(quyuTab.getID()) != null) {
				tabSet.selectTab(quyuTab);
			} else {
				quyuTab = new QuyuTab();
				quyuTab.setTitle("区域设置");
				tabSet.addTab(quyuTab);
				tabSet.selectTab(quyuTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_GROUP_SETTING)) {
			if (groupTab != null && tabSet.getTab(groupTab.getID()) != null) {
				tabSet.selectTab(groupTab);
			} else {
				groupTab = new GroupTab();
				groupTab.setTitle("用户组设置");
				tabSet.addTab(groupTab);
				tabSet.selectTab(groupTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_GROUP_AUTHORITY)) {
			if (groupQxTab != null && tabSet.getTab(groupQxTab.getID()) != null) {
				tabSet.selectTab(groupQxTab);
			} else {
				groupQxTab = new GroupQxTab();
				groupQxTab.setTitle("用户组权限");
				tabSet.addTab(groupQxTab);
				tabSet.selectTab(groupQxTab);

			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_SHOP_YAOHUO)) {
			if (yaohuoTab != null && tabSet.getTab(yaohuoTab.getID()) != null) {
				tabSet.selectTab(yaohuoTab);
			} else {
				yaohuoTab = null;
				yaohuoTab = new YaohuoTab();
				yaohuoTab.setTitle("门店要货");
				tabSet.addTab(yaohuoTab);
				tabSet.selectTab(yaohuoTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_WULIU_YANSHOU)) {
			if (yanshouTab != null && tabSet.getTab(yanshouTab.getID()) != null) {
				tabSet.selectTab(yanshouTab);
			} else {
				yanshouTab = null;
				yanshouTab = new YanshouTab();
				yanshouTab.setTitle("物流验收");
				tabSet.addTab(yanshouTab);
				tabSet.selectTab(yanshouTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_PEISONG)) {
			if (peisongTab != null && tabSet.getTab(peisongTab.getID()) != null) {
				tabSet.selectTab(peisongTab);
			} else {
				peisongTab = null;
				peisongTab = new PeisongTab();
				peisongTab.setTitle("配送");
				tabSet.addTab(peisongTab);
				tabSet.selectTab(peisongTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_WULIU_FANCHANG)) {
			if (fanchangTab != null && tabSet.getTab(fanchangTab.getID()) != null) {
				tabSet.selectTab(fanchangTab);
			} else {
				fanchangTab = null;
				fanchangTab = new FanchangTab();
				fanchangTab.setTitle("物流返厂");
				tabSet.addTab(fanchangTab);
				tabSet.selectTab(fanchangTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_YIKU)) {
			if (tuihuoTab != null && tabSet.getTab(tuihuoTab.getID()) != null) {
				tabSet.selectTab(tuihuoTab);
			} else {
				tuihuoTab = null;
				tuihuoTab = new TuihuoTab();
				tuihuoTab.setTitle("配送退货");
				tabSet.addTab(tuihuoTab);
				tabSet.selectTab(tuihuoTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_PANDIAN)) {
			if (pandianTab != null && tabSet.getTab(pandianTab.getID()) != null) {
				tabSet.selectTab(pandianTab);
			} else {
				pandianTab = null;
				pandianTab = new PandianTab();
				pandianTab.setTitle("盘点");
				tabSet.addTab(pandianTab);
				tabSet.selectTab(pandianTab);
			}
		} else if (nodeID.equals(Constants.TREE_ID_OF_ZIYOU)) {
			if (ziyouTab != null && tabSet.getTab(ziyouTab.getID()) != null) {
				tabSet.selectTab(ziyouTab);
			} else {
				ziyouTab = null;
				ziyouTab = new ZiyouTab();
				ziyouTab.setTitle("自由补货");
				tabSet.addTab(ziyouTab);
				tabSet.selectTab(ziyouTab);
			}
		}
	}

	private class ConfirmBooleanCallback implements BooleanCallback {
		private TabCloseClickEvent event;

		public ConfirmBooleanCallback(TabCloseClickEvent event) {
			this.event = event;
		}

		@Override
		public void execute(Boolean value) {
			if (value != null && value) {
				Tab tab = event.getTab();
				tabSet.removeTab(tab);
				tab = null;
			}
		}
	}
}
